package com.yin.demo01;

// 基本买票例子
public class SaleTicketDemo01 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.saleTicket();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.saleTicket();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 1; i < 60; i++) {
                ticket.saleTicket();
            }
        }, "C").start();
    }
}


// 单独的资源类 OOP编程
class Ticket {
    // 属性、方法
    private int ticketNum = 50;

    // 买票的方式
    public synchronized void saleTicket() {
        if (ticketNum > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (ticketNum--) + "张票,剩余：" + ticketNum);
        }
    }
}
